package com.xiaoyun.controller;

import com.xiaoyun.bo.MerchantOrderBO;
import com.xiaoyun.bo.ShopcartBO;
import com.xiaoyun.bo.SubmitOrderBO;
import com.xiaoyun.constant.Const;
import com.xiaoyun.enums.OrderStatusEnum;
import com.xiaoyun.enums.PayMethod;
import com.xiaoyun.pojo.OrderStatus;
import com.xiaoyun.service.OrderService;
import com.xiaoyun.service.PaymentOrderService;
import com.xiaoyun.utils.CookieUtils;
import com.xiaoyun.utils.JsonUtils;
import com.xiaoyun.utils.RedisUtil;
import com.xiaoyun.utils.XYJSONResult;
import com.xiaoyun.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Api("订单相关")
@RestController
@RequestMapping("orders")
@RequiredArgsConstructor
public class OrdersController extends BaseController {
    private final OrderService orderService;

    private final PaymentOrderService paymentOrderService;

    private final RedisUtil redisUtil;


    @ApiOperation("用户下单")
    @PostMapping("/create")
    public XYJSONResult create(@RequestBody SubmitOrderBO submitOrderBO,
                               HttpServletRequest request,
                               HttpServletResponse response) {
        if (submitOrderBO.getPayMethod() != PayMethod.WEIXIN.type &&
                submitOrderBO.getPayMethod() != PayMethod.ALIPAY.type) {
            return XYJSONResult.errorMsg("支付方式不支持");
        }
        String shoppartJson = redisUtil.getKey(Const.FOODIE_SHOPCART + ":" + submitOrderBO.getUserId());
        if (StringUtils.isBlank(shoppartJson)) {
            return XYJSONResult.errorMsg("购物车数据不正确");
        }
        List<ShopcartBO> shopcartList = JsonUtils.jsonToList(shoppartJson, ShopcartBO.class);

        OrderVO orderVO = orderService.createOrder(shopcartList, submitOrderBO);
        String orderId = orderVO.getOrderId();
        MerchantOrderBO merchantOrderBO = orderVO.getMerchantOrderBO();
        merchantOrderBO.setReturnUrl(Const.payReturnUrl);

        // redis之后, 完善购物车中已结算的商品
        shopcartList.removeAll(orderVO.getToBeRemovedShopcartList());
        redisUtil.setKey(Const.FOODIE_SHOPCART + ":" + submitOrderBO.getUserId(), JsonUtils.objectToJson(shopcartList));
        CookieUtils.setCookie(request, response, Const.FOODIE_SHOPCART, JsonUtils.objectToJson(shopcartList), true);
        //创建订单
        boolean isSuccess = false;
        try {
            isSuccess = paymentOrderService.createPaymentOrder(merchantOrderBO);
        } catch (Exception e) {
            e.printStackTrace();
            XYJSONResult.errorMsg(e.getMessage());
        }
        if (!isSuccess) {
            return XYJSONResult.errorMsg("创建订单失败");
        }


        return XYJSONResult.ok(orderId);
    }

    @PostMapping("notifyMerchantOrderPaid")
    public Integer notifyMerchantOrderPaid(String merchantOrderId) {
        orderService.updateOrderStatus(merchantOrderId, OrderStatusEnum.WAIT_DELIVER.type);
        return HttpStatus.OK.value();
    }

    @PostMapping("getPaidOrderInfo")
    public XYJSONResult getPaidOrderInfo(String orderId) {
        OrderStatus orderStatus = orderService.queryOrderStatusInfo(orderId);
        return XYJSONResult.ok(orderStatus);
    }
}
